
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.2 服务端 · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../客户端/" />
    
    
    <link rel="prev" href="../核心接口/" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    第一章 smart-socket开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../chapter-1/2.1-基础应用/">
            
                <a href="../../chapter-1/2.1-基础应用/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../chapter-1/2.2-Spring集成/">
            
                <a href="../../chapter-1/2.2-Spring集成/">
            
                    
                    1.2 Spring集成smart-socket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../../chapter-1/SUMMARY.html">
            
                <a href="../../chapter-1/SUMMARY.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../">
            
                <a href="../">
            
                    
                    第二章 源码解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../核心接口/">
            
                <a href="../核心接口/">
            
                    
                    2.1 核心接口与状态机
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3.2" data-path="./">
            
                <a href="./">
            
                    
                    2.2 服务端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../客户端/">
            
                <a href="../客户端/">
            
                    
                    2.3 客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../AioSession/">
            
                <a href="../AioSession/">
            
                    
                    2.4 会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../SUMMARY.html">
            
                <a href="../SUMMARY.html">
            
                    
                    2.5 小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../chapter-3/">
            
                <a href="../../chapter-3/">
            
                    
                    第三章 通信协议
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../chapter-3/1-消息归类/">
            
                <a href="../../chapter-3/1-消息归类/">
            
                    
                    3.1 消息归类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.2 半包/粘包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.3 Http消息解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.4 RPC消息解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../chapter-5/">
            
                <a href="../../chapter-5/">
            
                    
                    第四章 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../chapter-6/">
            
                <a href="../../chapter-6/">
            
                    
                    第五章 未来规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../end/">
            
                <a href="../../end/">
            
                    
                    附录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-http 实战手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../../smart-http/">
            
                <a href="../../../smart-http/">
            
                    
                    关于smart-http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" >
            
                <span>
            
                    
                    第一章 smart-http开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../../smart-http/chapter-1/">
            
                <a href="../../../smart-http/chapter-1/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../../smart-http/chapter-1/http_route.html">
            
                <a href="../../../smart-http/chapter-1/http_route.html">
            
                    
                    1.2 请求路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="../../../smart-http/chapter-1/file_upload.html">
            
                <a href="../../../smart-http/chapter-1/file_upload.html">
            
                    
                    1.3 文件上传/下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="../../../smart-http/chapter-1/websocket.html">
            
                <a href="../../../smart-http/chapter-1/websocket.html">
            
                    
                    1.4 WebSocket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../../share/firstfit/readme.html">
            
                <a href="../../../share/firstfit/readme.html">
            
                    
                    内存管理-首次适应算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../../share/thread/readme.html">
            
                <a href="../../../share/thread/readme.html">
            
                    
                    Thread知多少
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../../donation.html">
            
                <a href="../../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >2.2 服务端</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x670D;&#x52A1;&#x7AEF;aioquickserver">&#x670D;&#x52A1;&#x7AEF;AioQuickServer</h2>
<p>&#x5F02;&#x6B65;&#x975E;&#x963B;&#x585E;&#x901A;&#x4FE1;&#x7684;&#x670D;&#x52A1;&#x7AEF;&#x5B9E;&#x73B0;&#x3002;&#x8FD9;&#x4E2A;&#x7C7B;&#x4E3B;&#x8981;&#x662F;&#x5BF9;JDK&#x63D0;&#x4F9B;&#x7684;AIO&#x901A;&#x4FE1;&#x7C7B;AsynchronousServerSocketChannel&#x3001;AsynchronousChannelGroup&#x8FDB;&#x884C;&#x5C01;&#x88C5;&#x3002;AioQuickServer&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x901A;&#x4FE1;&#x7684;&#x8C03;&#x5EA6;&#x4E2D;&#x5FC3;&#xFF0C;&#x5728;&#x5B8C;&#x6210;&#x534F;&#x8BAE;&#x3001;&#x6D88;&#x606F;&#x5904;&#x7406;&#x5668;&#x7684;&#x5B9A;&#x4E49;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;AioQuickServer&#x6765;&#x542F;&#x52A8;&#x6211;&#x4EEC;&#x7684;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x3002;AioQuickServer&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x5FC5;&#x8981;&#x7684;&#x53C2;&#x6570;&#x914D;&#x7F6E;&#x63A5;&#x53E3;&#xFF0C;&#x65B9;&#x4FBF;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x8FDB;&#x884C;&#x8D44;&#x6E90;&#x5206;&#x914D;&#x4EE5;&#x8FBE;&#x5230;&#x6700;&#x4F18;&#x6548;&#x679C;&#x3002;</p>
<p>2.5.1 &#x6210;&#x5458;&#x5C5E;&#x6027;</p>
<table>
<thead>
<tr>
<th>&#x5C5E;&#x6027;&#x540D;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>serverSocketChannel</td>
<td>AsynchronousServerSocketChannel</td>
<td>JDK&#x63D0;&#x4F9B;&#x7684;AIO&#x670D;&#x52A1;&#x7AEF;&#x6838;&#x5FC3;&#x7C7B;</td>
</tr>
<tr>
<td>asynchronousChannelGroup</td>
<td>AsynchronousChannelGroup</td>
<td>JDK&#x4E3A;AIO&#x63D0;&#x4F9B;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x670D;&#x52A1;</td>
</tr>
<tr>
<td>config</td>
<td>IoServerConfig</td>
<td>&#x5B58;&#x50A8;AioQuickServer&#x670D;&#x52A1;&#x914D;&#x7F6E;&#x9879;</td>
</tr>
<tr>
<td>aioReadCompletionHandler</td>
<td>ReadCompletionHandler</td>
<td>smart-socket&#x63D0;&#x4F9B;&#x7684;IO&#x8BFB;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x7C7B;</td>
</tr>
<tr>
<td>aioWriteCompletionHandler</td>
<td>WriteCompletionHandler</td>
<td>smart-socket&#x63D0;&#x4F9B;&#x7684;IO&#x5199;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x7C7B;</td>
</tr>
<tr>
<td>bufferPool</td>
<td>BufferPagePool</td>
<td>&#x5185;&#x5B58;&#x6C60;&#x5BF9;&#x8C61;</td>
</tr>
<tr>
<td>workerExecutorService</td>
<td>ThreadPoolExecutor</td>
<td>Worker&#x7EBF;&#x7A0B;&#x6C60;</td>
</tr>
</tbody>
</table>
<p>2.5.2 &#x914D;&#x7F6E;&#x63A5;&#x53E3;</p>
<table>
<thead>
<tr>
<th>&#x65B9;&#x6CD5;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>public AioQuickServer setBannerEnabled(boolean bannerEnabled)</td>
<td>&#x670D;&#x52A1;&#x542F;&#x52A8;&#x65F6;&#x662F;&#x5426;&#x6253;&#x5370;smart-socket banner</td>
</tr>
<tr>
<td>public AioQuickServer setThreadNum(int num)</td>
<td>Server&#x670D;&#x52A1;&#x7EBF;&#x7A0B;&#x6570;</td>
</tr>
<tr>
<td>public AioQuickServer setReadBufferSize(int size)</td>
<td>&#x8BBE;&#x7F6E;AioSession&#x8BFB;&#x7F13;&#x5B58;&#x533A;&#x957F;&#x5EA6;</td>
</tr>
<tr>
<td>public AioQuickServer setOption(SocketOption socketOption, V value)</td>
<td>&#x8BBE;&#x7F6E;Socket&#x7684;TCP&#x53C2;&#x6570;&#x914D;&#x7F6E;</td>
</tr>
</tbody>
</table>
<p>2.5.3 &#x6838;&#x5FC3;&#x65B9;&#x6CD5;</p>
<p><strong>2.5.3.1 start&#xFF1A;&#x542F;&#x52A8;AIO&#x670D;&#x52A1;&#x7AEF;</strong></p>
<ul>
<li><p>&#x7247;&#x6BB5;&#x4E00;</p>
<pre><code class="lang-java">asynchronousChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(config.getThreadNum(), <span class="hljs-keyword">new</span> ThreadFactory() {
    <span class="hljs-keyword">byte</span> index = <span class="hljs-number">0</span>;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Thread <span class="hljs-title">newThread</span><span class="hljs-params">(Runnable r)</span> </span>{
         <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Thread(r, <span class="hljs-string">&quot;smart-socket:AIO-&quot;</span> + (++index));
    }
});
</code></pre>
<p>&#x521D;&#x59CB;&#x5316;AIO&#x670D;&#x52A1;&#x7684;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x7EC4;&#x5E76;&#x8D4B;&#x503C;&#x4E8E;<code>AioQuickServer</code>&#x6210;&#x5458;&#x5C5E;&#x6027;<code>asynchronousChannelGroup</code></p>
</li>
<li><p>&#x7247;&#x6BB5;&#x4E8C;</p>
<pre><code class="lang-java"><span class="hljs-keyword">this</span>.serverSocketChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup).bind(<span class="hljs-keyword">new</span> InetSocketAddress(config.getPort()), <span class="hljs-number">1000</span>);
</code></pre>
<p>&#x8FD9;&#x884C;&#x4EE3;&#x7801;&#x5F88;&#x76F4;&#x89C2;&#xFF0C;&#x6253;&#x5F00;AIO&#x670D;&#x52A1;&#x901A;&#x9053;&#x5E76;&#x7ED1;&#x5B9A;&#x7AEF;&#x53E3;&#x53F7;&#xFF0C;&#x4F46;&#x8981;&#x6CE8;&#x610F;bind&#x65B9;&#x6CD5;&#x3002;<code>AsynchronousServerSocketChannel</code>&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;bind&#x63A5;&#x53E3;&#xFF1A;bind(SocketAddress local)&#xFF0C;bind(SocketAddress local, int backlog)</p>
<p>&#x5982;&#x679C;&#x8C03;&#x7528;bind(SocketAddress local)&#x65B9;&#x6CD5;&#xFF0C;AsynchronousServerSocketChannel&#x5185;&#x90E8;&#x5B9E;&#x9645;&#x4E0A;&#x6267;&#x884C;&#x7684;&#x662F;bind(SocketAddress local, 0)&#x3002;&#x7136;&#x800C;backlog&#x7684;&#x503C;&#x5C0F;&#x4E8E;1&#x65F6;&#xFF0C;JDK&#x4F1A;&#x5C06;&#x5176;&#x9ED8;&#x8BA4;&#x8BBE;&#x7F6E;&#x4E3A;50&#x3002; backlog&#x7EF4;&#x62A4;&#x4E86;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#x961F;&#x5217;&#x957F;&#x5EA6;&#xFF0C;&#x5982;&#x679C;&#x961F;&#x5217;&#x6EE1;&#x65F6;&#x6536;&#x5230;&#x8FDE;&#x63A5;&#x6307;&#x793A;&#xFF0C;&#x5219;&#x62D2;&#x7EDD;&#x8BE5;&#x8FDE;&#x63A5;&#x3002;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;backlog&#x8BBE;&#x7F6E;&#x4E3A;50&#xFF0C;&#x5F53;&#x524D;&#x6709;50&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#x8FC7;&#x6765;&#xFF0C;&#x670D;&#x52A1;&#x7AEF;&#x8FD8;&#x672A;&#x6267;&#x884C;&#x8FD9;&#x4E9B;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#x7684;accept&#x65B9;&#x6CD5;&#x3002;&#x6B64;&#x65F6;&#x518D;&#x6709;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#x8FC7;&#x6765;&#xFF0C;&#x5219;&#x4F1A;&#x88AB;&#x62D2;&#x7EDD;&#x8FDE;&#x63A5;&#x3002;&#x9664;&#x975E;&#x8BF7;&#x6C42;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x8FDE;&#x63A5;&#x5B8C;&#x6210;accept&#x64CD;&#x4F5C;&#x5E76;&#x91CA;&#x653E;&#x51FA;&#x961F;&#x5217;&#x8D44;&#x6E90;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x624D;&#x53EF;&#x63A5;&#x53D7;&#x65B0;&#x7684;&#x8FDE;&#x63A5;&#x3002;</p>
</li>
<li><p>&#x7247;&#x6BB5;&#x4E09;</p>
</li>
</ul>
<pre><code class="lang-java">acceptThread = <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() {
    NetMonitor&lt;T&gt; monitor = config.getMonitor();

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">while</span> (running) {
            Future&lt;AsynchronousSocketChannel&gt; future = serverSocketChannel.accept();
            <span class="hljs-keyword">try</span> {
                <span class="hljs-keyword">final</span> AsynchronousSocketChannel channel = future.get();
                workerExecutorService.execute(<span class="hljs-keyword">new</span> Runnable() {
                    <span class="hljs-meta">@Override</span>
                    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
                        <span class="hljs-keyword">if</span> (monitor == <span class="hljs-keyword">null</span> || monitor.acceptMonitor(channel)) {
                            createSession(channel);
                        } <span class="hljs-keyword">else</span> {
                            config.getProcessor().stateEvent(<span class="hljs-keyword">null</span>, StateMachineEnum.REJECT_ACCEPT, <span class="hljs-keyword">null</span>);
                            LOGGER.warn(<span class="hljs-string">&quot;reject accept channel:{}&quot;</span>, channel);
                            closeChannel(channel);
                        }
                    }
                });
            } <span class="hljs-keyword">catch</span> (Exception e) {
                LOGGER.error(<span class="hljs-string">&quot;AcceptThread Exception&quot;</span>, e);
            }

        }
    }
}, <span class="hljs-string">&quot;smart-socket:AcceptThread&quot;</span>);
acceptThread.start();

<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">createSession</span><span class="hljs-params">(AsynchronousSocketChannel channel)</span> </span>{
    <span class="hljs-comment">//&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x5219;&#x6784;&#x9020;AIOSession&#x5BF9;&#x8C61;</span>
    AioSession&lt;T&gt; session = <span class="hljs-keyword">null</span>;
    <span class="hljs-keyword">try</span> {
        session = aioSessionFunction.apply(channel);
        session.initSession();
    } <span class="hljs-keyword">catch</span> (Exception e1) {
        LOGGER.debug(e1.getMessage(), e1);
        <span class="hljs-keyword">if</span> (session == <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">try</span> {
                channel.shutdownInput();
            } <span class="hljs-keyword">catch</span> (IOException e) {
                LOGGER.debug(e.getMessage(), e);
            }
            <span class="hljs-keyword">try</span> {
                channel.shutdownOutput();
            } <span class="hljs-keyword">catch</span> (IOException e) {
                LOGGER.debug(e.getMessage(), e);
            }
            <span class="hljs-keyword">try</span> {
                channel.close();
            } <span class="hljs-keyword">catch</span> (IOException e) {
                LOGGER.debug(<span class="hljs-string">&quot;close channel exception&quot;</span>, e);
            }
        } <span class="hljs-keyword">else</span> {
            session.close();
        }
    }
}
</code></pre>
<p>smart-socket&#x901A;&#x8FC7;&#x542F;&#x52A8;AcceptThread&#x7EBF;&#x7A0B;&#x540C;&#x6B65;&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;&#xFF0C;
&#x4E00;&#x65E6;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x4E0A;&#x6765;&#x4FBF;&#x751F;&#x6210;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x7531;WorkerThread&#x7EBF;&#x7A0B;&#x6C60;&#x6765;&#x521D;&#x59CB;&#x5316;AioSession&#x3002;
&#x6240;&#x6709;&#x7684;AioSession&#x5171;&#x7528;aioReadCompletionHandler&#x3001;aioWriteCompletionHandler&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x670D;&#x52A1;&#x7AEF;&#x4EA7;&#x751F;&#x7684;&#x5BF9;&#x8C61;&#x6570;&#x3002;</p>
<p><strong>2.5.3.2 shutdown&#xFF1A;&#x505C;&#x6B62;AIO&#x670D;&#x52A1;&#x7AEF;</strong></p>
<p>AIO&#x670D;&#x52A1;&#x505C;&#x6B62;&#x7684;&#x903B;&#x8F91;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x5173;&#x95ED;Channel&#x901A;&#x9053;&#xFF0C;&#x505C;&#x6B62;&#x7EBF;&#x7A0B;&#x7EC4;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">shutdown</span><span class="hljs-params">()</span> </span>{
    running = <span class="hljs-keyword">false</span>;
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">if</span> (serverSocketChannel != <span class="hljs-keyword">null</span>) {
            serverSocketChannel.close();
            serverSocketChannel = <span class="hljs-keyword">null</span>;
        }
    } <span class="hljs-keyword">catch</span> (IOException e) {
        LOGGER.warn(e.getMessage(), e);
    }
    <span class="hljs-keyword">if</span> (!workerExecutorService.isTerminated()) {
        <span class="hljs-keyword">try</span> {
            workerExecutorService.shutdownNow();
        } <span class="hljs-keyword">catch</span> (Exception e) {
            LOGGER.error(<span class="hljs-string">&quot;shutdown exception&quot;</span>, e);
        }
    }
    <span class="hljs-keyword">if</span> (!asynchronousChannelGroup.isTerminated()) {
        <span class="hljs-keyword">try</span> {
            asynchronousChannelGroup.shutdownNow();
        } <span class="hljs-keyword">catch</span> (IOException e) {
            LOGGER.error(<span class="hljs-string">&quot;shutdown exception&quot;</span>, e);
        }
    }
    <span class="hljs-keyword">try</span> {
        asynchronousChannelGroup.awaitTermination(<span class="hljs-number">3</span>, TimeUnit.SECONDS);
    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
        LOGGER.error(<span class="hljs-string">&quot;shutdown exception&quot;</span>, e);
    }
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-07-07 14:06:53
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../核心接口/" class="navigation navigation-prev " aria-label="Previous page: 2.1 核心接口与状态机">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../客户端/" class="navigation navigation-next " aria-label="Next page: 2.3 客户端">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.2 服务端","level":"1.2.3.2","depth":3,"next":{"title":"2.3 客户端","level":"1.2.3.3","depth":3,"path":"smart-socket/chapter-2/客户端/README.md","ref":"smart-socket/chapter-2/客户端/README.md","articles":[]},"previous":{"title":"2.1 核心接口与状态机","level":"1.2.3.1","depth":3,"path":"smart-socket/chapter-2/核心接口/README.md","ref":"smart-socket/chapter-2/核心接口/README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/book/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/chapter-2/服务端/README.md","mtime":"2019-07-07T06:06:53.508Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2020-04-02T05:22:17.739Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

